package com.kaibai.controller;

import com.kaibai.pojo.Page;
import com.kaibai.pojo.User;
import com.kaibai.service.UserService;
import com.kaibai.service.impl.UserServiceImpl;
import com.kaibai.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class userServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<User> page = userService.page(pageNo,pageSize);
        page.setUrl("user?action=page");
        request.setAttribute("userPage",page);
        request.getRequestDispatcher("/jsp/manager/user_manager.jsp").forward(request,response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),0);
        pageNo+=1;
        String name = request.getParameter("name");
        Integer age = WebUtil.parseInt(request.getParameter("age"),0);
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        String password = request.getParameter("password");
        User user = new User(null,name,age,email,sex,password);
        userService.insert(user);
        response.sendRedirect(request.getContextPath()+"/manage/user?action=page&pageNo="+pageNo);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        userService.deleteById(id);
        //response.sendRedirect(request.getContextPath()+"/manage/good?action=list");
        response.sendRedirect(request.getContextPath()+"/manage/user?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        User user = userService.getUserById(id);
        request.setAttribute("user",user);
        request.getRequestDispatcher("/jsp/manager/user_edit.jsp").forward(request,response);
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtil.parseInt(request.getParameter("id"),0);
        String name = request.getParameter("name");
        Integer age = WebUtil.parseInt(request.getParameter("age"),0);
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        String password = request.getParameter("password");
        User user = new User(id,name,age,email,sex,password);
        userService.update(user);
        response.sendRedirect(request.getContextPath()+"/manage/user?action=page&pageNo="+request.getParameter("pageNo"));
    }
}
